package com.glorysoft.hlgdpda.ui.trackin.viewmodel

import androidx.databinding.ObservableArrayList
import androidx.lifecycle.MutableLiveData
import com.glorysoft.hlgdpda.R
import com.glorysoft.hlgdpda.base.BaseViewModel
import com.glorysoft.hlgdpda.entity.ui.CallObserveBean
import com.glorysoft.hlgdpda.entity.LotEntity
import com.glorysoft.hlgdpda.entity.MLotListEntity
import com.glorysoft.hlgdpda.entity.ui.ToastMessageBean
import com.glorysoft.hlgdpda.model.TaskRepository
import com.google.gson.JsonObject
import com.glorysoft.hlgdpda.BR
import me.tatarka.bindingcollectionadapter2.ItemBinding

/**
 * FileName: TrackInViewModel
 * Author: XJH
 * Date: 2021/5/14 16:05
 * Description:
 * History:
 */
class TrackInViewModel : BaseViewModel() {

    val materialList: List<String> = listOf("No.", "物料批号", "物料描述", "数量", "特征码", "操作")
    val carrierList: List<String> = listOf("No.", "料盒号", "料盒类型", "层数", "容量", "操作")
    val lotList: List<String> = listOf("No.", "批次号", "工序", "批次数量", "批次状态")
    val batchList: List<String> = listOf("No.", "批次号", "产品名称", "产品描述", "数量", "工步名称","载具ID")

    var lotEntity = MutableLiveData<LotEntity>()
    private val trackInRepository = TaskRepository()

    //批次列表
    val lotItems = ObservableArrayList<LotEntity.LotInfoEntity>()

    //物料列表
    var mLotItems = ObservableArrayList<MLotListEntity.MLotEntity.MLotInfo>()

    //batch 批次列表
    var batchItems = ObservableArrayList<LotEntity.LotInfoEntity>()
    private val itemMaterialOnClickListener = object : OnItemClickListener {
        override fun onItemClick(item: Any) {
            defUI.callObserve.value = CallObserveBean(1, item)
        }
    }

    private val itemLotOnClickListener = object : OnItemClickListener {
        override fun onItemClick(item: Any) {
            defUI.callObserve.value = CallObserveBean(2, item)
        }
    }


    var mMaterielBinding =
        ItemBinding.of<MLotListEntity.MLotEntity.MLotInfo>(BR.item, R.layout.item_mlot)
            .bindExtra(BR.deleteListener, itemMaterialOnClickListener)

    var mLotBinding = ItemBinding.of<LotEntity.LotInfoEntity>(BR.lotItem, R.layout.item_lot)
        .bindExtra(BR.lotDeleteListener, itemLotOnClickListener)

    var mBatchBinding = ItemBinding.of<LotEntity.LotInfoEntity>(BR.batchItem, R.layout.item_batch)



    fun searchWorkInfo(lot: String, eqp: String) {
        val map = JsonObject()
        map.addProperty("WORKORDER", lot)
        map.addProperty("EQUIPMENTID", eqp)
        launchOnlyResult({
            val lotInfo = trackInRepository.getWorkInfo(map)
            lotInfo.LOTLIST.forEach {
                it.position = (lotInfo.LOTLIST.indexOf(it) + 1).toString()
                lotItems.add(it)
            }
            defUI.callObserve.value = CallObserveBean(200, "")
        })
    }

    /**
     * 列表删除信息
     */
    fun deleteInfo(type: String, item: Any) {
        when (type) {
            "material" -> mLotItems.remove(item as MLotListEntity.MLotEntity)
            "lot" -> {
                lotItems.remove(item as LotEntity)
                defUI.callObserve.value = CallObserveBean(200, "")
            }
        }
    }



    fun workTrackIn(eqpID: String,workNo : String) {
        val map = JsonObject()
        map.addProperty("ACTIONTYPE", "NoEqpTrackIn")
        //todo://   是否检查工步权限
        map.addProperty("ISCHECKSTEPAUTHORITY", true)
        map.addProperty("EQUIPMENTID", eqpID)
        map.addProperty("WORKORDER", workNo)
        launchOnlyResult({
            trackInRepository.workTrackIn(map)
            defUI.toastEvent.value = ToastMessageBean("进站成功", true)
            lotItems.clear()
            defUI.startActivity.call()
        })
    }

    /**
     * 获取batch信息
     *
     */
    fun searchBatchInfo(batch: String) {
        val map = JsonObject()
        map.addProperty("BATCHID", batch)
        launchOnlyResult({
            batchItems.clear()
            val mLotInfo = trackInRepository.getBatchInfo(map)
            mLotInfo.LOTLIST.forEach {
                it.position = (mLotInfo.LOTLIST.indexOf(it) + 1).toString()
                batchItems.add(it)
            }
            defUI.callObserve.value = CallObserveBean(210,batchItems.size)
        })
    }

    fun batchTrackIn2(batch: String, eqpID: String) {
        val map = JsonObject()
        map.addProperty("ACTIONTYPE", "DefaultTrackIn")
        map.addProperty("EQUIPMENTID", eqpID)
        map.addProperty("BATCHID", batch)
        launchOnlyResult({
            trackInRepository.batchTrackIn(map)
            defUI.toastEvent.value = ToastMessageBean("进站成功", true)
            batchItems.clear()
            defUI.callObserve.value = CallObserveBean(220,"")
        })
    }

    interface OnItemClickListener {
        fun onItemClick(item: Any)
    }
}